﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace Wicresoft.SMS.Data
{
    public class UserRoleService : IUserRoleService
    {
        IConnectStringProvider _csp;
        public UserRoleService(IConnectStringProvider provider)
        {
            _csp = provider;
        }

        //the return mustn't null
        public string[] GetRolesForUser(string username)
        {
            throw new NotImplementedException();
        }

        public bool ValidationUser(string key,string password)
        {
            if (key == null || password == null)
                return false;
            using (var db = new Entities(_csp.ConnectionString))
            {
                //System.Data.Objects.ObjectParameter op = new System.Data.Objects.ObjectParameter("reutrnUserID",System.Data.DbType.Int32);
               var user = db.cp_login(key).ToList<User>();

               if (user!=null && user.Count() > 0)
                    return true;
                return false;
            }
        }


        public long[] GetRolesValueForUser(string username)
        {
            List<long> roleslist = new List<long>();
            if (username == null)
                return roleslist.ToArray();
            using (var db = new Entities(_csp.ConnectionString))
            {
                var user = db.User.FirstOrDefault(p=>p.CIL == username);
                if (user != null && user.Role != null){
                    foreach (var role in user.Role){
                        roleslist.Add(role.RoleValue);
                    }
                }
            }
            return roleslist.ToArray();
        }

        public void LogIn(string username, bool remember)
        {
            //since username might be id alias and email
            FormsAuthentication.SetAuthCookie(username, remember);
        }

        public string GetRedirectUrl(string UserName, bool remember)
        {
            //!! if querystring and form are both not contain ReturnUrl , this may probably happen using ajax post
            // the FormsAuthentication.GetRedirectUrl will return FormsAuthentication.DefaultUrl
            string url = FormsAuthentication.GetRedirectUrl(UserName, remember);
            if (url == FormsAuthentication.DefaultUrl)
                return null;
            return url;
        }



        public User GetUserInfo(string key)
        {
            if (string.IsNullOrEmpty(key))
                return null;
            User user;
            using (var db = new Entities(_csp.ConnectionString))
            {
                int keyid;
                if (int.TryParse(key, out keyid)){
                    user = db.User.FirstOrDefault(p => (p.IsActive == 4 && (p.CIL == key || p.Email == key || p.UserID == keyid)));
                }
                else{
                    user = db.User.FirstOrDefault(p => (p.IsActive == 4 && (p.CIL == key || p.Email == key)));
                }
                if (user != null)
                {
                    user.Role.Load();
                    var temp2 = user.Department;
                }
            }
            return user;
        }


        public void LogOut()
        {
            FormsAuthentication.SignOut();
        }


        public string GetGetEmailByCSL(string csl)
        {
            if (csl != null && csl != "")
            {
                using (var dbacc = new Entities(_csp.ConnectionString))
                {
                    var user = dbacc.User.FirstOrDefault(u => u.CSL.ToLower() == csl.ToLower());
                    return user != null ? user.Email != null ? user.Email : "" : "";
                }
            }
            else
                return "";
        }

        public bool CheckEmail(string email)
        {
            if (email != null && email != "")
            {
                using (var dbacc= new Entities(_csp.ConnectionString))
                {
                    var em = dbacc.User.FirstOrDefault(u => u.Email.ToLower() == email.ToLower() && u.IsActive == 4);
                    return em != null ? true : false;                   
                }
            }
            else
                return false;
        }


        public string GetMobileByCSL(string csl)
        {
            if (!string.IsNullOrEmpty(csl))
            {
                using (var dbacc = new Entities(_csp.ConnectionString))
                {
                    var user = dbacc.User.FirstOrDefault(u => u.CSL.ToLower() == csl.ToLower());
                    return user != null ? user.Mobile != null ? user.Mobile : "" : "";
                }
            }
            else
                return "";
        }


        public string GetCILByCSL(string csl)
        {
            if (!string.IsNullOrEmpty(csl))
            {
                using (var dbacc = new Entities(_csp.ConnectionString))
                {
                    var user = dbacc.User.FirstOrDefault(u => u.CSL.ToLower() == csl.ToLower());
                    return user != null ? user.CIL : "";
                }
            }
            else
                return "";
        }

        public string GetCSLByCIL(string cil)
        {
            if (!string.IsNullOrEmpty(cil))
            {
                using (var dbacc = new Entities(_csp.ConnectionString))
                {
                    var user = dbacc.User.FirstOrDefault(u => u.CIL.ToLower() == cil.ToLower());
                    return user != null ? user.CSL : "";
                }
            }
            else
                return "";
        }
        
        public bool AuthUser(string username)
        {
            if (!string.IsNullOrEmpty(username))
            {
                using (var dbacc = new Entities(_csp.ConnectionString))
                {                    
                    var user = dbacc.User.FirstOrDefault(u => u.CIL.ToLower() == username.Trim().ToLower() && u.IsActive == 4);
                    return user != null ? true : false;
                }
            }
            else
                return false;
        }
    }
}
